package workforce.limousine.struts.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import limousine.dbservice.ImageService;
import limousine.util.Config;
import limousine.util.Dir;
import limousine.util.Func;
import limousine.util.Service;
import limousine.util.Token;
import limousine.vo.Image;
import limousine.vo.UploadImage;
import limousine.vo.Users;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.upload.FormFile;

public class UploadImageInsert extends Action {

    private String errorMsg = "Process Fail";
    private Func func = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***UploadImageInsert");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if (!user.getRole_name().equals("Admin")) {
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/


        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        Dir dir = new Dir();
        String target = Config.DIR_CONTENT_ROOT + dir.getFileSeperator() + Config.DIR_IMAGE + dir.getFileSeperator();
        String temp = Config.DIR_CONTENT_ROOT + dir.getFileSeperator() + Config.DIR_TMP + dir.getFileSeperator();


        UploadImage f = (UploadImage) form;

        ImageService is = new ImageService();
        is.setDataSource(ds);

        Image o = new Image();
        o.setTitle(func.jsp2UTF(f.getTitle()));
        o.setDetail(func.jsp2UTF(f.getDetail()));                
        o.setEnable("1");//fix
        o.setUpdateBy(user.getId());
        o.setCategory(f.getCategory());
        o.setTbRefId(session.getId());
        o.setType(f.getType());
        


        FormFile iFile = f.getiFile();
        if(iFile != null){
            System.out.println("FileName:"+iFile.getFileName());
            //o.setName(func.getPrefixFileName()+"-"+iFile.getFileName());

            //get TH name
            String name = func.jsp2UTF(f.getName());
            String [] x = name.split("\\\\");
            if(x.length>0){
                name = x[x.length-1];
            }
            //get TH name
            o.setName(func.getPrefixFileName()+"-"+name);
            System.out.println("NewFileName:"+o.getName());
            if (dir.writeFile(iFile, temp + o.getName())) {
                o.setContentType(iFile.getContentType());
            }
        }

        if (is.insert(o)) {
            dir.moveFile(temp + o.getName(), target + o.getName());


            session.setAttribute(Token.MSG_USER, Token.MSG_UPLOAD_SUCCESS);

//            return mapping.findForward(Token.SUCCESS);
        } else {            
            session.setAttribute(Token.MSG_ERROR, Token.MSG_UPLOAD_FAIL);
//            return mapping.findForward(Token.FAIL);
        }


        ActionForward af = new ActionForward("uploadimageinit.do?catId="+f.getCategory()+"&catLock="+f.isDisable(), true);
        return af;

    }

    private String checkEmptyString(String value) {// optimize data
        value = (value == null) ? "" : value.trim();
        return func.jsp2UTF(value);
    }
}
